package com.softline.documentsmall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softline.documentsmall.dao.ErrorCorrectionDetailsMapper;
import com.softline.documentsmall.dao.ErrorCorrectionMapper;
import com.softline.documentsmall.entity.ErrorCorrection;
import com.softline.documentsmall.entity.ErrorCorrectionDetails;
import com.softline.documentsmall.entity.ErrorCorrectionDetailsVO;
import com.softline.documentsmall.entity.ErrorDetails;
import com.softline.documentsmall.service.ErrorCorrectionDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: documentsmall
 * @description:
 * @author: wmj
 * @create: 2020-12-15
 * @ClassName: ErrorCorrectionDetailsServiceImpl
 */
@Slf4j
@Service
public class ErrorCorrectionDetailsServiceImpl extends ServiceImpl<ErrorCorrectionDetailsMapper, ErrorCorrectionDetails> implements ErrorCorrectionDetailsService {

    @Autowired
    ErrorCorrectionDetailsMapper errorCorrectionDetailsMapper;
    @Autowired
    ErrorCorrectionMapper errorCorrectionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ErrorCorrectionDetailsVO errorCorrectionDetailsVO) {
        String passportId = errorCorrectionDetailsVO.getPassportId();
        //记录一次纠错信息
        ErrorCorrection errorCorrection = new ErrorCorrection();
        errorCorrection.setPassportId(passportId);
        errorCorrection.setDescribe("进行了纠错");
        errorCorrection.setCreateTime(new Date());
        errorCorrectionMapper.insert(errorCorrection);
        //纠错详细信息
        List<ErrorDetails> errorDetailsList = errorCorrectionDetailsVO.getErrorDetails();
        List<ErrorCorrectionDetails> list = new ArrayList<>();
        for (int i = 0; i < errorDetailsList.size(); i++) {
            ErrorCorrectionDetails errorCorrectionDetails = new ErrorCorrectionDetails();
            ErrorDetails errorDetails = errorDetailsList.get(i);
            errorCorrectionDetails.setDictionaryId(errorCorrectionDetailsVO.getDictionaryId());
            errorCorrectionDetails.setType(errorCorrectionDetailsVO.getType());
            errorCorrectionDetails.setErrorCorrectionId(errorCorrection.getId());
            UUID uuid = UUID.randomUUID();
            String submitId = uuid.toString().replace("-","");
            errorCorrectionDetails.setSubmitId(submitId);
            errorCorrectionDetails.setEntryDetailsId(errorDetails.getEntryDetailsId());
            errorCorrectionDetails.setErrorContent(errorDetails.getErrorContent());
            errorCorrectionDetails.setRevisionComments(errorDetails.getRevisionComments());
            errorCorrectionDetails.setCreateTime(new Date());
            list.add(errorCorrectionDetails);
        }
        this.saveBatch(list);
    }

    @Override
    public List<ErrorCorrectionDetails> selectCorrectionList(Integer pageNo, Integer pageSize, String type) {
        List<ErrorCorrectionDetails> errorCorrectionDetails = errorCorrectionDetailsMapper.selectCorrectionList((pageNo - 1) * pageSize, pageSize, type);
        return errorCorrectionDetails;
    }
}
